import { Form, Validator } from 'spd-components/form';
import { Input, Form as AntdForm } from 'antd';
import { message } from 'antd';
import { IEnterpriseStruct, OriganizationUnit } from 'spd-request/organization-unit'
export type FormProps = {
    onSuccessAdd: (data: any) => void;
    onCancel: () => void; 
    department: IEnterpriseStruct|null|undefined;
    rootEnterpriceId: string;
    originData?: any;
    // parentId: number;
}
export function OrgForm(props: FormProps) {
    function  onSubmit(params: {displayName: string}&{[key: string]: any}) {
        OriganizationUnit.createOrUpdateOrganizationUnitExtend(params).then(({error, data}) => {
            if(error) {
                message.error(error)
            } else {
                props.onSuccessAdd(data);
                message.success('成功添加部门');
            }
        });
    }
    function  onCancel() {
        props.onCancel();
    }
    let [formInstance] = AntdForm.useForm();
    if(props.originData) {
        formInstance.setFieldsValue(props.originData);
    }
    return (
        <Form.Ctx.Provider value={{onSubmit: onSubmit, formInstance: formInstance, onCancel: onCancel}}>
            <Form.Modal title="添加部门">
                <Form.Container>
                    <Form.Item 
                        labelName="部门名称" 
                        name="displayName" 
                        rule={[
                            Validator.MaxLength('名称过长', 20),
                            Validator.MinLength('最少不小于2个字', 4)
                        ]}
                    >
                        <Input></Input>
                    </Form.Item>
                </Form.Container>
            </Form.Modal>
        </Form.Ctx.Provider>
    )
}